//
//  PaperDetailVC.m
//  Doctor
//
//  Created by 阳光 on 16/9/10.
//  Copyright © 2016年 阳光. All rights reserved.
//  论文详情界面

#import "PaperDetailVC.h"
#import "CommentListVC.h"

@interface PaperDetailVC ()<UIScrollViewDelegate>

@property(nonatomic,strong)UIScrollView *scrollView;

@property(nonatomic,strong)NSDictionary *paperDetailDic;

@property(nonatomic,strong)UIView *mainView;

@property(nonatomic,assign)CGFloat h;

@end

@implementation PaperDetailVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.automaticallyAdjustsScrollViewInsets=NO;
    
    self.title=@"详情";
    
    [self initNavBarBtnItem];
    
    [self getDoctorPublicListDetail];
    
}

-(void)initScrollView
{
    _scrollView=[[UIScrollView alloc]initWithFrame:CGRectMake(0,0,APPW,APPH)];
    _scrollView.backgroundColor=WhiteColor;
    _scrollView.showsVerticalScrollIndicator=NO;
    [self.view addSubview:_scrollView];
    
    _mainView=[self setMainView];
    setViewCoord_Y(_mainView,0);
    [_scrollView addSubview:_mainView];
    _scrollView.contentSize=CGSizeMake(APPW,CGRectGetMaxY(self.mainView.frame)+kmargin*10);
    
}

-(UIView *)setMainView
{
    UIView *mainView=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,0)];
    mainView.backgroundColor=WhiteColor;
    
    UIView *topLine=[[UIView alloc]initWithFrame:CGRectMake(0,0,APPW,1)];
    [mainView addSubview:topLine];
    topLine.backgroundColor=MAIN_BACK_COLOR;
    setViewHeight_Add(mainView,1);
    
    // title
    UILabel *titleLabel=[mainView getLabelWithText:_paperDetailDic[@"title"] font:Font_Number(17) textColor:Color000000 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    titleLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW-kmargin*2,35);
    [mainView addSubview:titleLabel];
     setViewHeight_Add(mainView,35+kmargin);
    
    // 时间
    NSDateFormatter *formatter=[[NSDateFormatter alloc] init];
    [formatter setDateStyle:NSDateFormatterMediumStyle];
    [formatter setTimeStyle:NSDateFormatterShortStyle];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    // 设置时区
    NSTimeZone *timeZone=[NSTimeZone timeZoneWithName:@"Asia/Shanghai"];
    [formatter setTimeZone:timeZone];
    // 时间戳转时间的方法
    NSDate *timesp=[NSDate dateWithTimeIntervalSince1970:[_paperDetailDic[@"create_time"] integerValue]];
    NSString *timesStr=[formatter stringFromDate:timesp];
    
    double timeWidth=[Tool getWidthByCurrentSize:timesStr withHeight:20 withWidthLimit:APPW/2 withFont:13];
    UILabel *timeLabel=[mainView getLabelWithText:timesStr font:Font_Number(11) textColor:Color909090 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
        timeLabel.frame=CGRectMake(kmargin,mainView.frame.size.height,timeWidth,20);
    [mainView addSubview:timeLabel];
   
    // user_name
    UILabel *userNameLabel=[mainView getLabelWithText:_paperDetailDic[@"user_name"] font:Font_Number(13) textColor:Main_Color textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:1];
    userNameLabel.frame=CGRectMake(CGRectGetMaxX(timeLabel.frame)+kmargin/2,mainView.frame.size.height,APPW/3,20);
    [mainView addSubview:userNameLabel];
     setViewHeight_Add(mainView,20);
    
    // img
    UIImageView *IMAGE=[[UIImageView alloc]initWithFrame:CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW-kmargin*2,300/2)];
    [mainView addSubview:IMAGE];
    [IMAGE sd_setImageWithURL:[NSURL URLWithString:_paperDetailDic[@"img"]] placeholderImage:[UIImage imageNamed:@"bigLogo"]];
    setViewHeight_Add(mainView,300/2+kmargin);
    
     // content
     CGSize contentSize=[_paperDetailDic[@"content"] boundingRectWithSize:CGSizeMake(APPW-kmargin*2,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:Font_Number(13) }context:nil].size;
    UILabel *contentLabel=[mainView getLabelWithText:_paperDetailDic[@"content"] font:Font_Number(13) textColor:Color333333 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
    contentLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin,APPW-kmargin*2,contentSize.height);
    [mainView addSubview:contentLabel];
    setViewHeight_Add(mainView,contentSize.height+kmargin);
    
    UILabel *saveLabel=[mainView getLabelWithText:[NSString stringWithFormat:@"%@ 次收藏",_paperDetailDic[@"sc_count"]] font:Font_Number(13) textColor:Color909090 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
    saveLabel.frame=CGRectMake(APPW-120,mainView.frame.size.height+kmargin,50,20);
    [mainView addSubview:saveLabel];
    
    UILabel *praiseLabel=[mainView getLabelWithText:[NSString stringWithFormat:@"%@ 次赞",_paperDetailDic[@"dz_count"]] font:Font_Number(13) textColor:Color909090 textAlignment:NSTextAlignmentRight lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
    praiseLabel.frame=CGRectMake(APPW-60,mainView.frame.size.height+kmargin,50,20);
    [mainView addSubview:praiseLabel];
    setViewHeight_Add(mainView,20+kmargin);
    
    if ([_paperDetailDic[@"pl"]count]>0)
    {
        UILabel *hotLabel=[mainView getLabelWithText:@"热门评论" font:Font_Number(13) textColor:Color909090 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        hotLabel.frame=CGRectMake(kmargin,mainView.frame.size.height+kmargin*3,APPW/2,20);
        [mainView addSubview:hotLabel];
        setViewHeight_Add(mainView,20+kmargin*3);
        
        UIImageView *hotUserNameUrl=[[UIImageView alloc]initWithFrame:CGRectMake(kmargin,mainView.frame.size.height+kmargin,kmargin*3,kmargin*3)];
        [mainView addSubview:hotUserNameUrl];
        ViewBorderRadius(hotUserNameUrl,kmargin*3/2,2,ClearColor);
        if (![_paperDetailDic[@"pl"][0][@"img"] isEqual:[NSNull null]])
        {
        [hotUserNameUrl sd_setImageWithURL:[NSURL URLWithString:_paperDetailDic[@"pl"][0][@"img"]] placeholderImage:[UIImage imageNamed:@"UserHeaderPlace"]];
        }
        else
        {
            hotUserNameUrl.image=[UIImage imageNamed:@"UserHeaderPlace"];
           
        }

        NSString *name;
        if (![_paperDetailDic[@"pl"][0][@"name"] isEqual:[NSNull null]])
        {
            name=_paperDetailDic[@"pl"][0][@"name"];
        }
        else
        {
            name=@"无名氏";
        }
        UILabel *hotUserNameLabel=[mainView getLabelWithText:name font:Font_Number(13) textColor:Color909090 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        hotUserNameLabel.frame=CGRectMake(CGRectGetMaxX(hotUserNameUrl.frame)+kmargin,mainView.frame.size.height+kmargin/2,APPW/2,20);
        [mainView addSubview:hotUserNameLabel];
        
        // 时间戳转时间的方法
        NSDate *timesp=[NSDate dateWithTimeIntervalSince1970:[_paperDetailDic[@"pl"][0][@"create_time"] integerValue]];
        NSString *timesStr=[formatter stringFromDate:timesp];
        
        UILabel *hotUserTimeLabel=[mainView getLabelWithText:timesStr font:Font_Number(11) textColor:Color909090 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        hotUserTimeLabel.frame=CGRectMake(CGRectGetMaxX(hotUserNameUrl.frame)+kmargin,CGRectGetMaxY(hotUserNameLabel.frame),APPW/2,20);
        [mainView addSubview:hotUserTimeLabel];
        
        setViewHeight_Add(mainView,kmargin*4);
        
         CGSize contentSize=[_paperDetailDic[@"pl"][0][@"content"] boundingRectWithSize:CGSizeMake(APPW-kmargin*2,CGFLOAT_MAX) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName:Font_Number(13) }context:nil].size;
        
        UILabel *hotUserContentLabel=[mainView getLabelWithText:_paperDetailDic[@"pl"][0][@"content"] font:Font_Number(13) textColor:Color909090 textAlignment:NSTextAlignmentLeft lineBreakMode:NSLineBreakByCharWrapping numberOfLines:0];
        hotUserContentLabel.frame=CGRectMake(CGRectGetMaxX(hotUserNameUrl.frame)+kmargin,mainView.frame.size.height+kmargin/2,APPW/2,contentSize.height);
        [mainView addSubview:hotUserContentLabel];
        
        setViewHeight_Add(mainView,contentSize.height);
    }
    
    UIView *midLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height+kmargin,APPW,1)];
    midLine.backgroundColor=MAIN_BACK_COLOR;
    [mainView addSubview:midLine];
    
    UIButton *moreBtn=[[UIButton alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height+kmargin, APPW,44)];
    [moreBtn setTitle:@"查看全部评论" forState:UIControlStateNormal];
    [moreBtn setTitleColor:Color909090 forState:UIControlStateNormal];
    [moreBtn.titleLabel setFont:Font_Number(13)];
    [moreBtn addTarget:self action:@selector(moreBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [mainView addSubview:moreBtn];
    setViewHeight_Add(mainView,44+kmargin);
    
    UIView *bottomLine=[[UIView alloc]initWithFrame:CGRectMake(0,mainView.frame.size.height,APPW,1)];
    bottomLine.backgroundColor=MAIN_BACK_COLOR;
    [mainView addSubview:bottomLine];
    return mainView;
}

// 跳转到更多评论列表界面
-(void)moreBtnClick
{
    CommentListVC *commentListVC=[CommentListVC new];
    commentListVC.paperID=_paperDetailDic[@"id"];
    [self.navigationController pushViewController:commentListVC animated:NO];
}

-(void)getDoctorPublicListDetail
{
    NSDictionary *dic=@{
                        @"doctor":[Util getUserDefaultObjectForKey:@"id"],
                        @"id":_paperID,
                        @"type_id":@(0)
                        };
    [QBRequest DoctorPublicListDetailWithDic:dic Success:^(id res)
     {
         _paperDetailDic=res[@"info"][0];
         
         [self initScrollView];
         
     } failure:^(NSString *error)
    {
         
     }];
}


-(void)initNavBarBtnItem
{
    self.navigationItem.leftBarButtonItem=[UIBarButtonItem initWithNormalImage:@"back_gray" target:self action:@selector(backItemEvent)];
}

-(void)backItemEvent
{
    [self.navigationController popViewControllerAnimated:NO];
}

@end
